package com.kaikai.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaikai.commonutils.R;
import com.kaikai.educms.entity.CrmBanner;
import com.kaikai.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-26
 */
@CrossOrigin
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    CrmBannerService bannerService;

    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    //分页查询banner
    @GetMapping("PageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        //创建page对象
        Page<CrmBanner> pageBanner=new Page<>(page,limit);
        bannerService.page(pageBanner, null);
        return R.ok().data("list",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }
    //每次操作数据库,清空缓存
    @CacheEvict(value = "banner", allEntries=true)
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return R.ok();
    }
    @CacheEvict(value = "banner", allEntries=true)
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok();
    }
    @CacheEvict(value = "banner", allEntries=true)
    @DeleteMapping("{id}")
    public R deleteBanner(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }

}

